﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Pages
{
    public partial class ServiceSearch : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["query"] != null)
                {
                    txtQuery.Text = Request["query"];
                    SearchService(Request["query"]);
                }
            }
        }
        //Search
        protected void lbtnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ServiceSearch.aspx?query=" + txtQuery.Text.Trim());
        }

        //搜索服务
        private void SearchService(string query)
        {
            //显示查询结果的最大数
            int max = 50;
            int bytelen = System.Text.Encoding.Default.GetByteCount(query);
            if (bytelen == 0)
            {
                base.NoticeInfo = "查询参数不能为空！";
            }
            else if (bytelen < 4)
            {
                base.NoticeInfo = "查询参数不得小于4个字节（两个中文字符或四个英文字符）！";
            }
            else
            {
                List<Model.ServiceInfo> searchResult = new List<Model.ServiceInfo>();
                IList<Model.ServiceInfo> tempResult = new List<Model.ServiceInfo>();
                //按姓名搜索
                tempResult = SearchName(query, max);
                if (tempResult != null)
                {
                    searchResult.AddRange(tempResult);
                }
                //如果结果数小于限制的最大数量
                if (searchResult.Count < max)
                {
                    //查询关键字匹配的结果
                    tempResult = SearchKey(query, max - searchResult.Count);
                    if (tempResult != null)
                    {
                        searchResult.AddRange(tempResult);
                    }
                }
                //如果结果数小于限制的最大数量
                if (searchResult.Count < max)
                {
                    //查询匹配标题的结果
                    tempResult = SearchTitle(query, max - searchResult.Count);
                    if (tempResult != null)
                    {
                        searchResult.AddRange(tempResult);
                    }
                }

                //如果结果数小于限制的最大数量
                if (searchResult.Count < max)
                {
                    //查询匹配描述信息的结果
                    tempResult = SearchDesciption(query, max - searchResult.Count);
                }

                UCServiceList1.Bind(searchResult);
                base.SuccessInfo = "查找 " + query + "...共有" + searchResult.Count.ToString() + "个相关结果";
            }
        }
        //匹配姓名
        private IList<Model.ServiceInfo> SearchName(string query, int count)
        {
            Model.UserInfo user = BLL.UserInfoBLL.Instance.GetDetailByName(query);
            if (user != null)
            {
                return BLL.ServiceInfoBLL.Instance.GetPagedServiceInfo("", "", "", user.UserName, 0, 1, 1, count, ref count);
            }
            else
            {
                return null;
            }
        }
        //匹配关键字
        private IList<Model.ServiceInfo> SearchKey(string query, int count)
        {
            Model.Keyword key = BLL.KeywordBLL.Instance.GetDetailByContent(query);
            if (key != null)
            {
                return BLL.ServiceInfoBLL.Instance.GetPagedServiceInfo("", key.Content, "", "", 0, 1, 1, count, ref count);
            }
            else
            {
                return null;
            }
        }
        // 匹配标题
        private IList<Model.ServiceInfo> SearchTitle(string query, int count)
        {
            return BLL.ServiceInfoBLL.Instance.GetListWhereTitleLike(query, count);
        }

        // 匹配描述
        private IList<Model.ServiceInfo> SearchDesciption(string query, int count)
        {
            return BLL.ServiceInfoBLL.Instance.GetListWhereDescLike(query, count);
        }



    }
}